package com.example.d10_homework.homework03;

import java.util.List;
import java.util.Random;

public class CjThread implements Runnable {
    private List<String> prizeList;

    public CjThread(List<String> prizeList) {
        this.prizeList = prizeList;
    }

    @Override
    public void run() {
        Random r = new Random();
        while (true) {
            synchronized (this) {
                // 1.获取线程的名称
                String name = Thread.currentThread().getName();
                // 2.如果集合中的元素为空,则结束抽奖
                if (prizeList.isEmpty()) {
                    break;
                }
                // 3.根据集合的长度去随机获取一个索引
                int index = r.nextInt(prizeList.size());
                // 4.每抽到一个奖品就将其丛集合中删去
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                // 5.打印抽到的奖品
                System.out.println(name + "抽到了一个:" + prizeList.remove(index));
            }
        }
    }
}
